package com.youzhi.backend.util;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.youzhi.backend.controller.BaseController;

import java.util.Collections;

public class CodeGenerate {


    public static String url = "jdbc:mysql://127.0.0.1:3306/youzhi?characterEncoding=UTF-8";
    public static String username = "root";
    public static String password = "123456";
    public static String author = "xumeng";
    public static String outputDirectory = "/Users/mengxu/IdeaProjects/youzhi/backend/src/main/java";


    public static void codeGenerate(String tableName) {


        FastAutoGenerator.create(url, username, password)
                .globalConfig(builder -> {
                    builder.author(author) // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            //.fileOverride() // 覆盖已生成文件
                            .dateType(DateType.ONLY_DATE) //设定日期类型字段
                            .outputDir(outputDirectory); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent("com.youzhi.backend") // 设置父包名
//                            .moduleName("system") // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, "/Users/mengxu/IdeaProjects/youzhi/backend/src/main/resources/mapper")); // 设置mapperXml生成路径
                })

                // 设置需要生成的表名
                .strategyConfig(builder -> builder.addInclude(tableName).build())

                //设置entity风格
                .strategyConfig(builder -> builder.entityBuilder().enableLombok().build())

                //设置Controller风格
                .strategyConfig(builder -> builder.controllerBuilder().superClass(BaseController.class).enableRestStyle().build())


//                 使用Freemarker引擎模板，默认的是Velocity引擎模板
//                .templateEngine(new FreemarkerTemplateEngine())
                .execute();

    }

    public static void main(String[] args) {
        codeGenerate("letter");
    }
}
